package com.sxt.util;

import java.util.Scanner;

/**
 * 输入工具类
 * @author lovemanai
 *
 */
public class InputTools {
	/*
	 * 检查输入的整数格式是否正确
	 */
	public static int getInt(String tip, Scanner input) {
		int val = 0;
		boolean isRight = true;
		System.out.print(tip);
		while(isRight) {
			try {
				val = Integer.parseInt(input.nextLine());
				isRight = false;
			} catch(NumberFormatException e) {
				System.out.println("输入格式有误，请重新输入！");
				System.out.println(tip);
			}
		}
		
		return val;
	}
	
	/*
	 * 检查输入的浮点数格式是否正确
	 */
	public static double getDouble(String tip, Scanner input) {
		double val = 0.0;
		boolean isRight = true;
		System.out.print(tip);
		while(isRight) {
			try {
				val = Double.parseDouble(input.nextLine());
				isRight = false;
			} catch(NumberFormatException e) {
				System.out.println("输入格式有误，请重新输入！");
				System.out.println(tip);
			}
		}
		
		return val;
	}
	
	/*
	 * 检查输入的字符串格式是否正确
	 */
	public static String getString(String tip, Scanner input) {
		String str = null;
		boolean isRight = true;
		System.out.print(tip);
		while(isRight) {
			str = input.nextLine();
			isRight = false;
		}
		
		return str;
	}
	
	
	
}
